package com.lanyou.esb.entity;

import java.util.Date;
import java.util.HashSet;
import java.util.Set;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EntityListeners;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Transient;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.apache.commons.beanutils.PropertyUtils;
import org.hibernate.annotations.GenericGenerator;
import org.springframework.data.annotation.CreatedBy;
import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.annotation.LastModifiedBy;
import org.springframework.data.annotation.LastModifiedDate;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.lanyou.cook.common.DefaultDateJsonSerializer;
import com.lanyou.cook.entity.domain.FieldSystem;
/**
 * 
 * @author ly-zengzugen
 *
 */


@Entity
@EntityListeners({ AuditingEntityListener.class})
@Table(name = "T_ESB_BU_IF_FTP")//还没有增加监听
public class Ftp{
	
	private String id;//FTP接口表ID
	
	private String systemCode;//系统编码
	
	private String interfaceName;//接口名称
	
	private String interfacecode;//接口编码
	
	private String version;//接口版本
	
	private String status;//接口状态
	
	private String desc;//接口描述
	
	private String overtime;//超时时长
	
	private String ifCost;//是否收费
	
	private String ifPublic;//是否公开
	
	private String isSchedule;//是否调度执行
	
	private String fileCachePath;
	
	private String type;// 接口类型
	
	private String remark;//备注
	
	private String auditStatus;//审核状态
	
	private String auditRemark;//审核意见
	
	private Set<FtpServer> ftpServers = new HashSet<FtpServer>();
	
	private Set<DefinedBundle> definedbundles = new HashSet<DefinedBundle>();
	
	private Set<FieldSystem> visibleFieldSystems = new HashSet<FieldSystem>();
	
	private Set<IfSchedule> ifSchedules = new HashSet<IfSchedule>();
	
	private String isEnable;
	
	private String creator;

	private String modifier;

	private Date createTime;

	private Date modifyTime;
	
	private FieldSystem fieldSystem;
	
	public final static Specification<Ftp> EmptySpecification = new Specification<Ftp>(){
		@Override
		public Predicate toPredicate(Root<Ftp> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
			return null;
		}
	};
	
	public static Specification<Ftp> idSpecification(String id){
		return new Specification<Ftp>(){
			@Override
			public Predicate toPredicate(Root<Ftp> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
				query.where(cb.equal(root.get("id"), id));
				return null;
			}
		};
	}
	
	
	
	@Id
	@GeneratedValue(generator = "uuidGenerator")
	@GenericGenerator(name = "uuidGenerator", strategy = "uuid")
	@Column(name = "IF_FTP_ID", length = 36)
	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}
	
	@Column(name="IF_STATUS")
	public String getStatus() {
		return status;
	}

	public void setStatus(String status) {
		this.status = status;
	}
	

	@Column(name="IF_DESC")
	public String getDesc() {
		return desc;
	}

	public void setDesc(String desc) {
		this.desc = desc;
	}

	

	@Column(name="IF_NAME")
	public String getInterfaceName() {
		return interfaceName;
	}

	public void setInterfaceName(String interfaceName) {
		this.interfaceName = interfaceName;
	}

	@Column(name="IF_CODE")
	public String getInterfacecode() {
		return interfacecode;
	}

	public void setInterfacecode(String interfacecode) {
		this.interfacecode = interfacecode;
	}

	@Column(name="SYSTEM_CODE")
	public String getSystemCode() {
		return systemCode;
	}

	public void setSystemCode(String systemCode) {
		this.systemCode = systemCode;
	}

	

	@Column(name="IF_VERSION")
	public String getVersion() {
		return version;
	}

	public void setVersion(String version) {
		this.version = version;
	}

	@Column(name="OVERTIME_TIMES")
	public String getOvertime() {
		return overtime;
	}

	public void setOvertime(String overtime) {
		this.overtime = overtime;
	}

	@Column(name="IS_COST")
	public String getIfCost() {
		return ifCost;
	}

	public void setIfCost(String ifCost) {
		this.ifCost = ifCost;
	}

	
	@Column(name = "IS_PUBLIC", length = 2)
	public String getIfPublic() {
		return ifPublic;
	}

	public void setIfPublic(String ifPublic) {
		this.ifPublic = ifPublic;
	}
	
	@Column(name = "IS_SCHEDULE", length = 2)
	public String getIsSchedule() {
		return isSchedule;
	}

	public void setIsSchedule(String isSchedule) {
		this.isSchedule = isSchedule;
	}


	@Column(name = "FILE_CACHE_PATH", length = 100)
	public String getFileCachePath() {
		return fileCachePath;
	}

	public void setFileCachePath(String fileCachePath) {
		this.fileCachePath = fileCachePath;
	}
	
	
	@Column(name = "IF_TYPE", length = 2)
	public String getType() {
		return type;
	}
	
	public void setType(String type) {
		this.type = type;
	}
	
	
	@Column(name="REMARK")
	public String getRemark() {
		return remark;
	}

	public void setRemark(String remark) {
		this.remark = remark;
	}

	@Column(name="AUDIT_STATUS")
	public String getAuditStatus() {
		return auditStatus;
	}

	public void setAuditStatus(String auditStatus) {
		this.auditStatus = auditStatus;
	}

	@Column(name="AUDIT_REMARK")
	public String getAuditRemark() {
		return auditRemark;
	}

	public void setAuditRemark(String auditRemark) {
		this.auditRemark = auditRemark;
	}
	
	
	@OneToMany(fetch = FetchType.EAGER, mappedBy = "ftp")
	@JsonIgnore
	public Set<FtpServer> getFtpServers() {
		return ftpServers;
	}

	public void setFtpServers(Set<FtpServer> ftpServers) {
		this.ftpServers = ftpServers;
	}

	@ManyToMany
	@JoinTable(name = "T_ESB_BU_IF_BUNDLES_RELATION", 
		joinColumns = @JoinColumn(name = "IF_ID", referencedColumnName = "IF_FTP_ID") , 
			inverseJoinColumns = @JoinColumn(name = "BUNDLE_ID", referencedColumnName = "BUNDLE_ID") 
	)
	public Set<DefinedBundle> getDefinedbundles() {
		return definedbundles;
	}

	public void setDefinedbundles(Set<DefinedBundle> definedbundles) {
		this.definedbundles = definedbundles;
	}
	
	
	@ManyToMany
	@JoinTable(name = "T_ESB_BU_IF_VISIBLITY", 
		joinColumns = @JoinColumn(name = "IF_ID", referencedColumnName = "IF_FTP_ID") , 
			inverseJoinColumns = @JoinColumn(name = "IF_VISIBLE_SYSTEM_ID", referencedColumnName = "FIELD_SYSTEM_ID") 
	)
	public Set<FieldSystem> getVisibleFieldSystems() {
		return visibleFieldSystems;
	}



	public void setVisibleFieldSystems(Set<FieldSystem> visibleFieldSystems) {
		this.visibleFieldSystems = visibleFieldSystems;
	}
	
	
	@OneToMany(fetch = FetchType.EAGER, mappedBy = "ftp")
	@JsonIgnore
	public Set<IfSchedule> getIfSchedules() {
		return ifSchedules;
	}


	public void setIfSchedules(Set<IfSchedule> ifSchedules) {
		this.ifSchedules = ifSchedules;
	}



	@Column(name = "IS_ENABLE", length = 2)
	public String getIsEnable() {
		return isEnable;
	}

	public void setIsEnable(String isEnable) {
		this.isEnable = isEnable;
	}
	
	
	@ManyToOne
	@JoinColumn(name = "FIELD_SYSTEM_ID")
	public FieldSystem getFieldSystem() {
		return fieldSystem;
	}

	public void setFieldSystem(FieldSystem fieldSystem) {
		this.fieldSystem = fieldSystem;
	}
	
	

	@CreatedBy
	@Column(length = 50, updatable=false)
	public String getCreator() {
		return creator;
	}

	public void setCreator(String creator) {
		this.creator = creator;
	}

	@LastModifiedBy
	@Column(length = 50)
	public String getModifier() {
		return modifier;
	}

	public void setModifier(String modifier) {
		this.modifier = modifier;
	}

	@Column(name = "CREATED_DATE", updatable=false)
	@JsonSerialize(using = DefaultDateJsonSerializer.class)
	@CreatedDate
	public Date getCreateTime() {
		return createTime;
	}

	public void setCreateTime(Date createTime) {
		this.createTime = createTime;
	}

	@Column(name = "LAST_UPDATED_DATE")
	@JsonSerialize(using = DefaultDateJsonSerializer.class)
	@LastModifiedDate
	public Date getModifyTime() {
		return modifyTime;
	}

	public void setModifyTime(Date modifyTime) {
		this.modifyTime = modifyTime;
	}
	
	@Transient
	public String getFieldSystemId() {
		try {
			return (String) PropertyUtils.getProperty(fieldSystem, "id");
		} catch (Exception e) {
			return null;
		}
	}
	
	
	@Transient
	public String getCreateTimeForExport(){
		if(createTime != null){
			return createTime.toString().substring(0, createTime.toString().length() - 2);
		}else{
			return "";
		}
		
	}
	
	@Transient
	public String getModifyTimeForExport(){
		if(modifyTime != null){
			return modifyTime.toString().substring(0, modifyTime.toString().length() - 2);
		}else{
			return "";
		}
	}
}